<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/1
 * Time: 22:11
 */

namespace Lib\Api;


class UtilsApi {

    /** 获取列表
     * @param $modelName
     * @param $where
     * @param $page_num
     * @param $page_size
     * @param $field
     * @param $order
     * @return mixed
     */
    public static function getList($modelName, $where, $page_num, $page_size, $field, $order='') {
        $model = M($modelName);
        return  $model->field($field)->where($where)->order($order)->page($page_num, $page_size)->select();//page两个参数（页码，条数），不需要自己计算
    }

    /** 获取总数
     * @param $modelName
     * @param $where
     * @return mixed
     */
    public static function getTotal($modelName, $where = array()) {
        $model = M($modelName);
        return $model->where($where)->count('id');
    }

    /** 获取详细信息
     * @param $modelName
     * @param $where
     * @param $field
     * @return mixed
     */
    public static function getInfo($modelName, $where, $field) {
        $model = M($modelName);
        return $model->field($field)->where($where)->find();
    }

    /** 添加数据
     * @param $modelName
     * @param $data
     * @return mixed
     */
    public static function add($modelName, $data) {
        $model = M($modelName);
        return $model->add($data);
    }

    /** 保存数据
     * @param $modelName
     * @param $data
     * @return bool
     */
    public static function save($modelName, $data) {
        $model = M($modelName);
        return $model->save($data);
    }

    /** 删除数据
     * @param $modelName
     * @param $where
     */
    public static function delete($modelName, $where) {
        $model = M($modelName);
        $model->where($where)->delete();
    }
}